<?php
use sendmsg\SendUtility;
use think\Db;
use think\Request;
use think\Loader;
//获取全局分类根据父id查询
function GetCurrentWholeTypein($string){
	$Where=array();
	$Where['TypeID']=(['in',$string]);
	$Where['TypeState']=1;
	$List=db::name('wholetype')->field('TypeID,TypeName')->where($Where)->order('TypeSort Asc,TypeID Asc')->select();
	return $List;
}
//获取城市分类
function GetCityTypeList($PID)
{
	$Where=array();
	$Where['TypePID']=$PID;
	$Where['TypeState']=1;
	$List=db::name('citytype')->where($Where)->order('TypeSort Asc,TypeID Asc')->select();
	return $List;
}

//自定义一维数组转二维数组
function change_arry($arry){
	$data=array();
	foreach($arry as $k=>$vl){
		$data[]=array(
			'name'=>$vl,
			'id'=>$k
		);
	}
	return $data;
}
/*****************************************************************/
//公共函数 匹配自定义数组checked选中状态
function ArrayCheck($kid,$fname){
	$data='';
	foreach($fname() as $k=>$vl){
		if($k==$kid){
			$data=$vl;
		}
	}
	return $data;
}
function wherepay(){
	return array('wxpay'=>'微信','alipay'=>'支付宝','yezf'=>'余额');
}
/*
  * 创建log记录
  * $data String 记录内容
  * $texname String text 名称
*/
function write_log($data,$texname,$type=1){
	$years = date('Y-m'); //设置路径目录信息
	$url = $years.'/'.date('Ymd').$texname;
	$dir_name=dirname($url); //目录不存在就创建
	if(!file_exists($dir_name)) {
		//iconv防止中文名乱码
		$res = mkdir(iconv("UTF-8", "GBK", $dir_name),0777,true);
	}
	$fp = fopen($url,"a");
	//打开文件资源通道 不存在则自动创建
	fwrite($fp,var_export($data,true)."\r\n\r\n");
	//写入文件
	fclose($fp);
	//关闭资源通道
}
//2019/11/21 星期四
function Send_Sms($mibile,$mtext){
	if($mibile){
		$utility = new SendUtility();
		$RtnString = $utility->Send($mibile,$mtext);
	}
}
//2019/11/21 星期四
function Send_Smsjs($mibile,$mtext,$UserNamejs,$Passwordjs,$Signaturejs){
	if($mibile){
		$utility = new SendUtility();
		$RtnString = $utility->Sendjs($mibile,$mtext,$UserNamejs,$Passwordjs,$Signaturejs);
	}
}
/***********************************************/
function httpCurl($url, $params, $method = 'GET', $header = array(), $multi = false){

	date_default_timezone_set('PRC');
	$opts = array(

		CURLOPT_TIMEOUT        => 30,

		CURLOPT_RETURNTRANSFER => 1,

		CURLOPT_SSL_VERIFYPEER => false,

		CURLOPT_SSL_VERIFYHOST => false,

		CURLOPT_HTTPHEADER     => $header,

		CURLOPT_COOKIESESSION  => true,

		CURLOPT_FOLLOWLOCATION => 1,

		CURLOPT_COOKIE

		=>session_name().'='.session_id(),

	);
	/* 根据请求类型设置特定参数 */
	switch(strtoupper($method)){
	case 'GET':
	// $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
	// 链接后拼接参数  &  非？
		$opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
		break;
	case 'POST'://判断是否传输文件
		//$params = $multi ? $params : http_build_query($params);
		$opts[CURLOPT_URL] = $url;
		$opts[CURLOPT_POST] = 1;
		$opts[CURLOPT_POSTFIELDS] = $params;
		break;

	default:
		throw new Exception('不支持的请求方式！');

	}
	/* 初始化并执行curl请求 */

	$ch = curl_init();

	curl_setopt_array($ch, $opts);

	$data  = curl_exec($ch);

	$error = curl_error($ch);

	curl_close($ch);

	if($error) throw new Exception('请求发生错误：' . $error);

	return  $data;

}
function generate_str( $length = 6) {
	// 字符集，可任意添加你需要的字符
	$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
	$str = '';
	for ( $i = 0; $i < $length; $i++ )
	{
		$str .= $chars[ mt_rand(0, strlen($chars) - 1) ];
	}
	return $str;
}
// 返回时间戳格式化日期时间，默认当前
function get_datetime($timestamp = null)
{
    if (! $timestamp)
        $timestamp = time();
    return date('Y-m-d H:i:s', $timestamp);
}
//分类指定字段
function Getcontent_info($id){
	$Where=array();
	$Where['TypeID']=$id;
	$result=db::name('wholetype')->where($Where)->value('TypeName');
	return $result;
}
//地区指定字段
function Getcity_info($id){
	$Where=array();
	$Where['TypeID']=$id;
	$result=db::name('citytype')->where($Where)->value('TypeName');
	return $result;
}

//判断链接是否包含http或https
function is_http($url){
    if(preg_match("/^(http:\/\/|https:\/\/).*$/",$url)){
        return 1;
    }else{
        return 0;
    }
}
function navlink($action,$navlink){

	foreach($navlink as $val){
		$navdata[]=$val['NewsAbstract'];
	}
	if (!in_array($action,$navdata)){
		$action='1';
	}
	return $action;
}
//查询pay状态
function get_paystate($mid){
	$Where=array();
	$data='';
	$Where['BalanceMemberID']=$mid;
	$result=db::name('balance')->where($Where)->value('BalancePayState');
	if($result==1){
		$data='<span style="color:#06a236">已支付</span>';
	}elseif($result==0){
		$data='<span style="color:#f90000">未支付</span>';
	}else{
		$data='<span style="color:#f90000">未支付</span>';
	}
	return $data;
}
//查询状态
function get_istate($isstate){
	if($isstate){
		return true;
	}else{
		return false;
	}
}
//判断是否 微信浏览器
function isWeixin() {
	if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
		return true;
	} else {
		return false;
	}
}
//获取语言
function get_lg() {
	return 'cn';
}